package me.ywy.dbtest.dongnaodb;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.util.List;

import me.ywy.dbtest.dongnaodb.bean.Person;
import me.ywy.dbtest.dongnaodb.db.BaseDao;
import me.ywy.dbtest.dongnaodb.db.BaseDaoFactory;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button mCreateDb;
    private Button mInsertDb;
    private BaseDao<Person> baseDao;
    private Button mUpdateDb;
    private Button mDeleteDb;
    private Button mQueryDb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void requestMyPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            String[] permission = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
            int checkWritePermission = ContextCompat.checkSelfPermission(this, permission[0]);
            int checkReadPermission = ContextCompat.checkSelfPermission(this, permission[1]);
            if (checkWritePermission != PackageManager.PERMISSION_GRANTED ||
                    checkReadPermission != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(permission, 2000);

            }
        }
    }

    private void initView() {
        mCreateDb = findViewById(R.id.create_db);
        mCreateDb.setOnClickListener(this);
        mInsertDb = findViewById(R.id.insert_db);
        mInsertDb.setOnClickListener(this);
        mUpdateDb = findViewById(R.id.update_db);
        mDeleteDb = findViewById(R.id.delete_db);
        mQueryDb = findViewById(R.id.query_db);
        mUpdateDb.setOnClickListener(this);
        mDeleteDb.setOnClickListener(this);
        mQueryDb.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.create_db:
                createDataBase();
                break;
            case R.id.insert_db:
                insertDataBase();
                break;
            case R.id.update_db:
                updateDataBase();
                break;
            case R.id.delete_db:
                deleteDatabase();
                break;
            case R.id.query_db:
                queryDataBase();
                break;
        }
    }

    private void queryDataBase() {
        Person person = new Person();
        person.setName("闫文勇");
        List<Person> query = baseDao.query(person);
        Log.d("dongnao_db" , "query:"+query.size()+" message are find");
        int i = 0;
        for (Person p:query) {
            Log.d("dongnao_db" , "i="+ i++ +  "," +p.toString());

        }
    }

    private void deleteDatabase() {
        Person p = new Person();
        p.setName("西门勇");
        int delete = baseDao.delete(p);
        Log.d("dongnao_db" , "delete:"+delete+" message are delete");

    }

    private void updateDataBase() {
        Person person = new Person();
        person.setName("闫文勇");
        person.setAge(31);
        long update = baseDao.update(new Person("西门勇", 19, "123", 231.1), person);
        Log.d("dongnao_db" , "update:"+update+" message are update");
    }

    private void insertDataBase() {
        Person person = new Person();
        person.setName("闫文勇");
        person.setAge(31);
        person.setIdNo("37140219870817001X");
        person.setMoney(14.6);
        baseDao.insert(person);
    }

    private void createDataBase() {
        requestMyPermission();
        baseDao = BaseDaoFactory.getInstance().getBaseDao(BaseDao.class,Person.class);
    }
}
